﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace PoolGame2.Menus
{
    public class GamerSignInMenu : Menu
    {
        bool offered = false;
#if XBOX
        MenuItem signInOne = null;
        MenuItem signInTwo = null;
#endif
        int iToSignIn = 0;

        public GamerSignInMenu(PoolGame game)
            : base(game)
        {
            int height = 50;
            int menuspacing = 70;
            MenuItem item = new MenuItem("Begin Debug Game", game);
            item.Position = new Vector2(40, height);
            item.onSelected = new EventHandler(beginDebugGame);
            AddMenuItem(item);
            //height += (item.GetHeight() * 2);
            height += menuspacing;
#if XBOX
            signInOne = new MenuItem("Sign In One Player To Live", game);
            signInOne.Position = new Vector2(40, height);
            signInOne.onSelected = new EventHandler(signinSelected);
            AddMenuItem(signInOne);
            //height += (signInOne.GetHeight() * 2);
            height += menuspacing;
            
            signInTwo = new MenuItem("Sign In Two Players To Live", game);
            signInTwo.Position = new Vector2(40, height);
            signInTwo.onSelected = new EventHandler(signinSelected);
            AddMenuItem(signInTwo);
            //height += (signInTwo.GetHeight() * 2);
            height += menuspacing;
#else
            MenuItem signIn = new MenuItem("Sign In To Live", game);
            signIn.Position = new Vector2(40, height);
            signIn.onSelected = new EventHandler(signinSelected);
            AddMenuItem(signIn);
            //height += (signIn.GetHeight() * 2);
            height += menuspacing;
#endif

            item = new MenuItem("Exit", game);
            item.Position = new Vector2(40, height);
            item.onSelected = new EventHandler(exitSelected);
            AddMenuItem(item);
        }

        public override void OnOpen()
        {
            iToSignIn = 1;
            offered = false;
            base.OnOpen();
        }

        public override void Update(GameTime gameTime)
        {
            if (Gamer.SignedInGamers.Count == iToSignIn)
            {
                PoolGame pGame = (PoolGame)Game;
                pGame.menuManager.PreviousMenu();
            }
            base.Update(gameTime);
        }

        public void beginDebugGame(object caller, EventArgs e)
        {
            PoolGame pgame = (PoolGame)Game;
            pgame.InitialiseGame(true);
        }

        public void signinSelected(object caller, EventArgs e)
        {
            if (offered == false)
            {
#if XBOX
                if (caller == signInTwo)
                    iToSignIn = 2;
                else
                    iToSignIn = 1;
#else
                iToSignIn = 1;
#endif
                offered = true;
                Guide.ShowSignIn(iToSignIn, false);

            }
        }

        public void exitSelected(object caller, EventArgs e)
        {
            Game.Exit();
        }
    }
}
